<?php
session_start();
include('../include/util.inc.php');
function normalizeString($str)
{
    $str = trim($str);
    $str = preg_replace('/\s+/', " ", $str);
    return strtolower($str);
}

$php_correct = $_SESSION['answer'];
$origin_user = $_SESSION['user_input'];
$php_user = $origin_user;
// $php_user = array();

// for($i = 0; $i < count($php_correct); $i++){
//     if(!isset($origin_user[$i])){
//         array_push($php_user, '');
//     }else{
//         array_push($php_user, $origin_user[$i]);
//     }
// }

$countdown = 0;

if (isset($_SESSION['countdown'])) {
    $countdown = $_SESSION['countdown'];
    $start_times = $_SESSION['start_times'];
    $end_times = $_SESSION['end_times'];
    $TLE = array();
    $time_interval = array();
}


$grade = 0;
$wrongid = array();


if (is_null($php_correct) || is_null($php_user)) {
    throw new Exception("JSON convert Array Failed");
} else {
    for ($i = 0; $i < count($php_correct); $i++) {
        if (isset($_SESSION['countdown'])) {
            array_push($time_interval, max(0, $end_times[$i] - $start_times[$i]));
        }
        if (isset($_SESSION['countdown']) && (max(0, $end_times[$i] - $start_times[$i]) > $countdown + 1)) {
            array_push($TLE, $i);
        } else if (normalizeString($php_correct[$i]) == normalizeString($php_user[$i])) {
            $grade += 1;
        } else {
            array_push($wrongid, $i);
        }
    }
}

if (isset($_SESSION['uid'])) {
    $PDO = getPDO();
    $uid = intval($_SESSION['uid']);
    $quiz_start_time = $_SESSION['quiz_start_time'];
    $hid = $PDO->quote(str_pad($uid, 4, '0', STR_PAD_LEFT) . date("YmdHis", $quiz_start_time));
    for ($i = 0; $i < count($wrongid); $i++) {
        $index = $wrongid[$i];
        $question = $PDO->quote($_SESSION['question'][$index]);
        $WA = $PDO->quote($php_user[$index]);
        $state = $PDO->quote('Wrong answer');
        $sql = "INSERT INTO detail (hid, dquestion, danswer, dstate) VALUES ($hid, $question, $WA, $state);";
        $PDO->query($sql);
    }
    if (isset($_SESSION['countdown'])) {
        for ($i = 0; $i < count($TLE); $i++) {
            $index = $TLE[$i];
            $question = $PDO->quote($_SESSION['question'][$index]);
            $WA = $PDO->quote($php_user[$index]);
            $state = $PDO->quote('Time limit exceeded');
            $sql = "INSERT INTO detail (hid, dquestion, danswer, dstate) VALUES ($hid, $question, $WA, $state);";
            $PDO->query($sql);
        }
    }
}

$result = ['grade' => $grade, 'total' => count($php_correct), 'wrongid' => $wrongid, 'user_input' => $php_user, 'correct' => $php_correct];
if (isset($_SESSION['countdown'])) {
    $result += ['countdown' => $countdown, 'time_interval' => $time_interval, 'start_times' => $start_times, 'end_times' => $end_times, 'TLE' => $TLE];
}
echo json_encode($result);

if(isset($_SESSION['countdown']))
{
    unset($_SESSION['countdown']);
    unset($_SESSION['start_times']);
    unset($_SESSION['end_times']);
    unset($_SESSION['record_start_time']);
}

unset($_SESSION['question']);
unset($_SESSION['answer']);

unset($_SESSION['user_input']);


?>